<script setup lang="ts">
// 删除计划
import {message, Modal} from "ant-design-vue";
import {WxtBizCode, WxtUtils} from "~/contentScripts/views/wxt/lib/wxtUtils";

const props = defineProps({
  rows: {
    type: Array,
    default: () => []
  },
  bizCode: {
    type: String as PropType<WxtBizCode>,
    default: WxtBizCode.关键字推广
  },
  type: {
    type: String,
    default: 'plan'
  },
  name: {
    type: String,
    default: '计划'
  },
});
const emit = defineEmits([
  'refresh',
]);
const handle = () => {
  if (props.rows.length === 0) {
    message.error("请选择需要批量处理的数据！");
    return;
  }
  Modal.confirm({
    title: '删除计划',
    content: '删除后将无法恢复，不可在列表页查询对应数据但报表页仍支持查询，请慎重操作？',
    okText: '确定',
    cancelText: '取消',
    onOk() {
      if (props.type === 'plan') {
        WxtUtils.deleteRecord(props.rows.map((r: any) => r.campaignId), props.bizCode).then(res => {
          if (res.data.info.ok !== true) {
            message.error(res.data.info.message)
            return;
          }
          message.success("删除计划成功！");
          emit('refresh');
        });
      } else if (props.type === 'unit') {
        WxtUtils.deleteRecord(undefined, props.bizCode, {
          adgroupList: props.rows.map((r: any) => ({
            campaignId: r.campaignId,
            adgroupId: r.adgroupId,
            materialId: r.material.materialId,
          }))
        }, 'adgroup').then(res => {
          if (res.data.info.ok !== true) {
            message.error(res.data.info.message)
            return;
          }
          message.success("删除"+props.name+"成功！");
          emit('refresh');
        });
      }
      // WxtUtils.campaignDelete(props.rows.map((r: any) => r.campaignId),props.bizCode).then(res => {
      //   if (res.data.info.ok !== true) {
      //     message.error(res.data.info.message)
      //     return;
      //   }
      //   message.success("删除计划成功！");
      //   emit('refresh');
      // });
    },
  });
};
</script>

<template>
  <div @click="handle">
    <slot>
      <a-button size="small" type="primary" danger ghost>
        删除{{name}}
      </a-button>
    </slot>
  </div>
</template>

<style scoped>

</style>
